﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using DomainModel;
using ObjectModel;

namespace DataAccess
{
    public class DataInitializer : DropCreateDatabaseAlways<DomainModel>
    {
        protected override void Seed(DomainModel context)
        {
            var testUser = new User
            {
                Name = "Test User",
                Email = "user@email",
                Created = DateTime.UtcNow,
                Comment = "Comment",
                DisplayName = "Test User",
                IsApproved = true,
                IsLockedOut = false,
                LastActivity = DateTime.UtcNow,
                LastLockout = null,
                LastLogin = DateTime.UtcNow,
                LastPasswordChange = null,
                Password = "aaa",
                Updated = DateTime.UtcNow,
            };

            context.Users.Add(testUser);

            var application = new Application
            {
                Description = "Forum Description", 
                Name = "Forum", 
                Link = "Forum",
                Controller = "Forum",
                MenuItems = new List<MenuItem>(),
                IsDefault = true,
                Enabled = true,
                Visible = true
            };

            application.MenuItems.Add(new MenuItem
            {
                Action = "Index",
                Name = "Sections",
                IsDefault = true, 
                Link = "Index",
                Visible = true,
                Enabled = true
            });

            application.MenuItems.Add(new MenuItem
            {
                Action = "Recent",
                Name = "Recent Posts",
                Link = "Recent",
                Visible = true,
                Enabled = true
            });

            application.MenuItems.Add(new MenuItem
            {
                Action = "NewThread",
                Name = "New Thread",
                Link = "NewThread",
                Visible = true,
                Enabled = true
            });

            application.MenuItems.Add(new MenuItem
            {
                Action = "Rules",
                Name = "Rules",
                Link = "Rules",
                Visible = true,
                Enabled = true
            });

            application.MenuItems.Add(new MenuItem
            {
                Action = "Search",
                Name = "Search",
                Link = "Search",
                Visible = true,
                Enabled = true
            });

            context.Applications.Add(application);

            application = new Application
            {
                Description = "User Profile",
                Name = "Profile",
                Link = "Profile",
                MenuItems = new List<MenuItem>(),
                Enabled = true,
                Visible = true,
                Controller = "Profile",
            };

            application.MenuItems.Add(new MenuItem
            {
                Action = "Index",
                IsDefault = true,
                Link = "General",
                Name = "General",
                Enabled = true,
                Visible = true
            });

            context.Applications.Add(application);

            application = new Application
            {
                Description = "Account",
                Name = "Account",
                Link = "Account",
                MenuItems = new Collection<MenuItem>(),
                Enabled = true,
                Visible = false,
                Controller = "Account"
            };

            context.Applications.Add(application);

            context.ConfigurationItems.Add(new ConfigurationItem {Id = "Name", Value = "Test Forum"});
            context.ConfigurationItems.Add(new ConfigurationItem {Id = "Description", Value = "Test Forum Description"});

            var section1 = new Section
            {
                Name = "Common Information",
                Description = "Description of common information",
                IsForum = false,
                Sections = new List<Section>()
            };

            context.Sections.Add(section1);

            var subSection1 = new Section
            {
                Name = "Common Section 1",
                Description = "Common Section Description",
                IsForum = true,
                Threads = new List<Thread>()
            };

            section1.Sections.Add(subSection1);

            var subSection2 = new Section
            {
                Name = "Common Section 2",
                Description = "Description of Common Section 2",
                IsForum = true
            };

            section1.Sections.Add(subSection2);

            var section2 = new Section
            {
                Name = "Private Information",
                Description = "Closed section",
                IsForum = false,
                Sections = new List<Section>()
            };

            context.Sections.Add(section2);

            var subSection3 = new Section
            {
                Name = "Private Section 1",
                Description = "Description of Private Section 1",
                IsForum = true
            };

            section2.Sections.Add(subSection3);

            var thread1 = new Thread
            {
                Author = testUser,
                Created = DateTime.UtcNow,
                Description = "Thread description",
                Name = "New Thread",
                Posts = new List<Post>()
            };

            thread1.Posts.Add(new Post
            {
                Author = testUser,
                Created = DateTime.UtcNow,
                Title = "Post Title",
                Text = "Post Message",
            });

            subSection1.Threads.Add(thread1);
        }
    }
}
